package com.nineshadow.games.launch;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.nineshadow.games.core.GameRoom;
import com.nineshadow.games.interfaces.IGameUser;
import com.nineshadow.games.util.WorkerThreadFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
public class GameWorld {
    /**
     * 游戏运行定时任务线程池
     */
    public static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors
            .newScheduledThreadPool(Runtime.getRuntime().availableProcessors(),
                    new WorkerThreadFactory("ScheduledThread_"));

    /**
     * 游戏运行线程池
     */
    public static final ExecutorService ROOM_EXECUTOR_SERVICE = Executors
            .newCachedThreadPool(new WorkerThreadFactory("GameThread_"));

    /**
     * 玩家轮询线程池
     */
    public static final ExecutorService USER_POLL_EXECUTOR_SERVICE = Executors
            .newCachedThreadPool(new WorkerThreadFactory("USER_POLL_"));

    /**
     * 用户id
     */
    public final static AttributeKey<Integer> userId_key = AttributeKey.valueOf("userId_key");
    /**
     * 不需要登陆就可以调用的方法
     */
    private static final HashSet<String> URLNameWithoutLogin = new HashSet<>();

    private static AtomicInteger roomId = new AtomicInteger(1);
    /**
     * 在线用户
     */
    private static ConcurrentHashMap<String, ChannelHandlerContext> sessionMap = new ConcurrentHashMap<>();

    /**
     * 登录用户
     */
    private static ConcurrentHashMap<Integer, IGameUser> loginUserMap = new ConcurrentHashMap<>();

    /**
     * 所有房间
     */
    private static ConcurrentHashMap<String, GameRoom> allRoom=new ConcurrentHashMap<>();

    //房间内玩家
    private static Multimap<String, Integer> roomUsers = ArrayListMultimap.create();

    static {
        URLNameWithoutLogin.add("game/login");
        URLNameWithoutLogin.add("game/onOpen");
        URLNameWithoutLogin.add("game/sendMsg");
    }

    public static boolean containsURL(final String URL) {
        return URLNameWithoutLogin.contains(URL);
    }

    public static ConcurrentHashMap<String, ChannelHandlerContext> getSessionMap() {
        return sessionMap;
    }

    public static IGameUser getOnlineUser(Integer userId) {
        if (Objects.isNull(userId)) {
            return null;
        }
        return loginUserMap.get(userId);
    }

    public static void putOnlineUser(Integer userId, IGameUser user) {
        loginUserMap.put(userId, user);
    }

    public static IGameUser quitLoginUser(Integer userId) {
        return loginUserMap.remove(userId);
    }

    public static void removeRoom(String roomId){
        log.info("移除游戏房间：{}",roomId);
        allRoom.remove(roomId);
    }

    public static GameRoom getRoom(String roomId){
        return allRoom.get(roomId);
    }
    public static void putRoom(String roomId,GameRoom room){
        allRoom.put(roomId,room);
    }

    public static int getRoomId() {
        return roomId.getAndIncrement();
    }
}
